﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinCourseControls.DAL;
using WinCourseControls.Models;

namespace WinCourseControls
{
    public partial class FrmTreeView02 : Form
    {
        public FrmTreeView02()
        {
            InitializeComponent();
        }

        MenuDAL menuDAL = new MenuDAL();
        private void FrmTreeView02_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();//清空节点
            //1.获取节点数据
            List<MenuInfo> menuList = menuDAL.GetModelList("", "", 0);
            //2.定义一个添加节点的方法（递归）：自己调用自己

            //3.调用添加方法，添加所有节点
            CreateNode(menuList, null, 0);

            treeView1.ExpandAll();//展开所有节点
        }

        /// <summary>
        /// 添加节点（递归）
        /// </summary>
        /// <param name="allList"></param>
        /// <param name="pNode"></param>
        /// <param name="parentId"></param>
        private void CreateNode(List<MenuInfo> allList,TreeNode pNode,int parentId)
        {
            //获取指定的子节点数据
            List<MenuInfo> subMenus = allList.Where(m => m.ParentId == parentId).ToList();
            if(subMenus.Count>0)
            {
                foreach(MenuInfo child in subMenus)
                {
                    TreeNode node = new TreeNode();
                    node.Name = child.MenuId.ToString();
                    node.Text = child.MenuName;
                    //该节点是根节点还是指定节点的子节点
                    if (pNode == null)
                    {
                        treeView1.Nodes.Add(node);
                    }
                    else
                        pNode.Nodes.Add(node);
                    //以node为父节点，去添加它的子节点
                    CreateNode(allList, node, child.MenuId);
                }
            }
        }

        /// <summary>
        /// 节点勾选或取消勾选时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
           
            TreeNode curNode = e.Node;
            SetCurNodeChecked(curNode, e.Action);

        }

        /// <summary>
        /// 处理当前节点的子节点的勾选状态   一层一层向下传递
        /// </summary>
        /// <param name="pNode"></param>
        private void SetChildNodesChecked(TreeNode pNode)
        {
            foreach (TreeNode node in pNode.Nodes)
            {
                node.Checked = pNode.Checked;
                SetChildNodesChecked(node);
            }
        }

        /// <summary>
        /// 当前节点的父节点的勾选处理   一层一层向上传递
        /// </summary>
        /// <param name="childNode"></param>
        private void SetParentNodeChecked(TreeNode childNode)
        {
            TreeNode parentNode = childNode.Parent;
            if (parentNode != null)
            {
                bool bl = false;
                foreach (TreeNode node in parentNode.Nodes)
                {
                    if (node.Checked)
                    {
                        bl = true;
                        break;
                    }
                }
                parentNode.Checked = bl;
                SetParentNodeChecked(parentNode);//以父节点为子节点去处理它的父节点
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            e.Node.Checked = !e.Node.Checked;
            TreeNode curNode = e.Node;
            SetCurNodeChecked(curNode, e.Action);
        }

        private void SetCurNodeChecked(TreeNode curNode,TreeViewAction action)
        {
            if (action == TreeViewAction.ByMouse || action == TreeViewAction.ByKeyboard)
            {
                //父节点：子节点的勾选
                SetChildNodesChecked(curNode);

                //子节点：父节点的勾选
                SetParentNodeChecked(curNode);
            }
        }
    }
}
